<?php
class arcService extends commonService{
	public function __construct(){
		parent::__construct();
		$this->table=table('arc');
		$this->table_err=table('arc_err');
		$this->table_fav=table('favorite');
	}
	
	//输出并更新文章点击次数
	public function update_arc_hits($id){
		if($id<=0) return false;
		$query="select `id`,`hits` from `{$this->table}` where `id`='{$id}' and `status`='1' limit 1";
		$r=$this->m->select($query);
		if(!$r) return false;
		$query="update `{$this->table}` set `hits`=`hits`+1 where `id`='{$id}' limit 1";
		$this->m->query($query);
		return "document.write('{$r['hits']}');";
	}
	//计算百分比 被多个方法调用
	private function dig_cal($row){
		if(!is_array($row)) return false;

		if($row['goodpost'] + $row['badpost'] == 0){
			$row['goodper'] = $row['badper'] = 0;
		}else{
			$row['goodper'] = number_format($row['goodpost'] / ($row['goodpost'] + $row['badpost']), 3) * 100;
			$row['badper'] = 100 - $row['goodper'];
		}
		$row['goodper'] = trim(sprintf("%4.2f", $row['goodper']));
		$row['badper'] = trim(sprintf("%4.2f", $row['badper']));
		return array('goodper'=>$row['goodper'],'badper'=>$row['badper']);
	}
	//文章的顶踩功能 ajax获取顶踩数据
	public function dig_data($arc_id,$open_cache=0){
		$data=array();
		//这里要采用缓存
		if($arc_id<=0) return $data;
		$query="select `id`,`goodpost`,`badpost` from `{$this->table}` where `id`='{$arc_id}' and `status`='1' limit 1";
		$row=$this->m->select($query);
		if(!$row) return $data;
		$r=$this->dig_cal($row);
		$row['goodper']=$r['goodper'];
		$row['badper']=$r['badper'];
		$data=$row;
		return $data;
	}
	//好评
	public function dig_good($arc_id){
		$data=array();
		$query="update `{$this->table}` set `goodpost`=`goodpost`+1 where `id`='{$arc_id}' and `status`='1' limit 1";
		$this->m->query($query);
		if($this->m->affected_rows()) $data=$this->dig_data($arc_id);
		return $data;
	}
	//差评
	public function dig_bad($arc_id){
		$data=array();
		$query="update `{$this->table}` set `badpost`=`badpost`+1 where `id`='{$arc_id}' and `status`='1' limit 1";
		$this->m->query($query);
		if($this->m->affected_rows()) $data=$this->dig_data($arc_id);
		return $data;
	}
	//模板
	public function dig_tpl($row){
		$digg = '<div class="diggbox digg_good" onmousemove="this.style.backgroundPosition=\'left bottom\';" onmouseout="this.style.backgroundPosition=\'left top\';" onclick="postDigg(\'good\','.$row['id'].')">
            <div class="digg_act">顶一下</div>
            <div class="digg_num">('.$row['goodpost'].')</div>
            <div class="digg_percent">
                <div class="digg_percent_bar"><span style="width:'.$row['goodper'].'%"></span></div>
                <div class="digg_percent_num">'.$row['goodper'].'%</div>
            </div>
        </div>
        <div class="diggbox digg_bad" onmousemove="this.style.backgroundPosition=\'right bottom\';" onmouseout="this.style.backgroundPosition=\'right top\';" onclick="postDigg(\'bad\','.$row['id'].')">
            <div class="digg_act">踩一下</div>
            <div class="digg_num">('.$row['badpost'].')</div>
            <div class="digg_percent">
                <div class="digg_percent_bar"><span style="width:'.$row['badper'].'%"></span></div>
                <div class="digg_percent_num">'.$row['badper'].'%</div>
            </div>
        </div>';
		return $digg;
	}
	//挑错部分
	public function pick_error_type(){
		$arr=array(
			1=>'错别字(除的、地、得)',
            2=>'成语运用不当',
			3=>'专业术语写法不规则',
			4=>'产品与图片不符',
			5=>'事实年代以及内容错误',
			6=>'技术参数错误',
			7=>'其他',
		);
		return $arr;
	}
	//查询文章信息
	public function pick_error($id){
		$query="select `id`,`title` from `{$this->table}` where `id`='{$id}' and `status`='1' limit 1";
		return $this->m->select($query);
	}
	//插入错误信息表
	public function pick_error_($post){
		$result=array();
		$post['arc_id']=intval($post['arc_id']);
		$post['type']=intval($post['type']);
		$post['timestring']=time();

        $vali=validate::check('arc_err',$post,'add');
        if($vali){
            $result[0]=$this->model->_i($this->table_err,$post);
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
	}
	//挑错部分
	//文章推荐
	public function recommend($id){
		$query="select `id`,`title`,`htmlurl` from `{$this->table}` where `id`='{$id}' and `status`='1' limit 1";
		return $this->m->select($query);
	}
	public function recommend_($post){
		$result=array();
		$post['arc_id']=intval($post['arc_id']);
		$post['msg']=trim(stripslashes($post['msg']));
		$post['email']=trim($post['email']);

        //发送email
		$title='你的好友给你推荐了一篇文章';
		$msg=nl2br(htmlspecialchars($post['msg']));
		
		$send_mail=new send_mail($this->conf->send_mail);
		$result[0]=$send_mail->send($post['email'],$title,$msg);
        return $result;
	}
	//文章收藏部分
	public function favorite($id){
		$query="select `id`,`title`,`htmlurl` from `{$this->table}` where `status`='1' and `id`='{$id}' limit 1";
		return $this->m->select($query);
	}
	public function favorite_($arc_id,$user_id,$d){
		$d['user_id']=intval($user_id);
		$d['timestring']=time();
		$d['arc_id']=intval($arc_id);
		
		$d['title']=addslashes($d['title']);
		$d['htmlurl']=addslashes($this->conf->WEB['url'].trim($d['htmlurl'],'/'));
		
		$query="select `id` from `{$this->table_fav}` where `arc_id`='{$d['arc_id']}' and `user_id`='{$d['user_id']}' limit 1";
		$r=$this->m->get_field($query);
		if($r>0) return true;
		
		return $this->model->_i($this->table_fav,$d);
	}
//
}